package ua.maxicash.vo;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.LazyInitializationException;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import ua.maxicash.vo.Work.WorkLight;

import javax.persistence.*;
import java.sql.Date;
import java.sql.Time;

@Entity
@Table(name = "WORKINGDAY")
@JsonIgnoreProperties({"work","workingDayLight"})
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class WorkingDay {

    public class WorkingDayLight {

        private Date workDate;
        private Time startTime;
        private Time endTime;
        private WorkLight work;

        public Date getWorkDate() {
            return workDate;
        }

        public void setWorkDate(Date workDate) {
            this.workDate = workDate;
        }

        public Time getStartTime() {
            return startTime;
        }

        public void setStartTime(Time startTime) {
            this.startTime = startTime;
        }

        public Time getEndTime() {
            return endTime;
        }

        public void setEndTime(Time endTime) {
            this.endTime = endTime;
        }

        public WorkLight getWork() {
            return work;
        }

        public void setWork(WorkLight work) {
            this.work = work;
        }

    }

    @Id
    @Column(name = "ID_WORKINGDAY")
    @GeneratedValue
    private Long idWorkingDay;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_WORK", insertable = false, updatable = false)
    private Work work;

    @Column(name = "WORKDATE")
    private Date workDate;

    @Column(name = "Start_time")
    private Time startTime;

    @Column(name = "end_time")
    private Time endTime;

    public WorkingDay() {
    }

    public Long getIdWorkingDay() {
        return idWorkingDay;
    }

    public void setIdWorkingDay(Long idWorkingDay) {
        this.idWorkingDay = idWorkingDay;
    }

    public Date getWorkDate() {
        return workDate;
    }

    public void setWorkDate(Date workDate) {
        this.workDate = workDate;
    }

    public Time getStartTime() {
        return startTime;
    }

    public void setStartTime(Time startTime) {
        this.startTime = startTime;
    }

    public Time getEndTime() {
        return endTime;
    }

    public void setEndTime(Time endTime) {
        this.endTime = endTime;
    }

    public Work getWork() {
        return work;
    }

    public void setWork(Work work) {
        this.work = work;
    }

    @Override
    public String toString() {
        return "WorkingDay [idWorkingDay=" + idWorkingDay + ", workDate="
                + workDate + ", startTime=" + startTime + ", endTime="
                + endTime + "]";
    }

    public WorkingDayLight getWorkingDayLight() {

        WorkingDayLight wdl = this.new WorkingDayLight();
        wdl.setEndTime(endTime);
        wdl.setStartTime(startTime);
        wdl.setWorkDate(workDate);
        if (work != null) {
            try {
                wdl.setWork(work.getWorkLight());
            } catch (LazyInitializationException e) {
                wdl.setWork(null);
                work = null;
            }
        }
        return wdl;

    }

    // ID_WORKINGDAY WORKDATE START_TIME END_TIME
}
